package com.atguigu.dga.governance.assess.spec;


import com.atguigu.dga.governance.assess.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.constant.CodeConst;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component("TABLE_NAME_STANDARD")
public class TableNameStandardAssessor extends Assessor {








    public static void main(String[] args) {
        String email = "windyzj@163.com";

        //构建表达式
        Pattern emailPattern = Pattern.compile("^\\w{2,20}@\\w{1,10}\\.(com|cn|org)$");
        //放入比较对象
        Matcher matcher = emailPattern.matcher(email);

        //进行比较
        if(matcher.matches()){
            System.out.println("符合规范");

        }else {
            System.out.println("不符合规范");
        }
    }

    //为不同的层次，构建不同的表达式
    Pattern odsPattern = Pattern.compile("^ods_\\w+_(inc|full)$");
    Pattern dimPattern = Pattern.compile("^dim_\\w+_(zip|full)$");
    Pattern dwdPattern = Pattern.compile("^dwd_\\w+_\\w+_(inc|full)$");
    Pattern dwsPattern = Pattern.compile("^dws_\\w+_\\w+_\\w+_(1d|nd|td)$");
    Pattern adsPattern = Pattern.compile("^ads_\\w+$");
    Pattern dmPattern = Pattern.compile("^dm_\\w+$");

    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws ParseException {
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        String dwLevel = tableMetaInfo.getTableMetaInfoExtra().getDwLevel();
        String tableName = tableMetaInfo.getTableName();
        Pattern tableNamePattern=null;
        // 根据要考评的表的层次 选择表达式
        if(dwLevel.equals(CodeConst.DW_LEVEL_ODS)){
            tableNamePattern=odsPattern;
        } else if (dwLevel.equals(CodeConst.DW_LEVEL_DWD)) {
            tableNamePattern=dwdPattern;
        } else if (dwLevel.equals(CodeConst.DW_LEVEL_DWS)) {
            tableNamePattern=dwsPattern;
        } else if (dwLevel.equals(CodeConst.DW_LEVEL_DIM)) {
            tableNamePattern=dimPattern;
        } else if (dwLevel.equals(CodeConst.DW_LEVEL_ADS)) {
            tableNamePattern=adsPattern ;
        } else if (dwLevel.equals(CodeConst.DW_LEVEL_DM)) {
            tableNamePattern=dmPattern;
        }else {
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf(5));
            governanceAssessDetail.setAssessProblem("未纳入分层");
            return;
        }

        // 利用表达式进行比较
        Matcher matcher = tableNamePattern.matcher(tableName);

        //如果不符合 给差评
        if(!matcher.matches()){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("不符合表名规范");
        }

    }




}
